<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
class WxRcodeController extends ApiBaseController
{
    public function index($data,$uid)
    {
        //$appid = 'wxa3a7eb6425a7973b';
        //$secret = '8c3fba37b82185f4105ed28df7aaefc5';
        $appid = 'wx6250a2d976e85a39';
        $secret = '6f9d267b526a0c203de27bcd44f7f5b9';
        $res = $this->getAccessToken($appid,$secret,'client_credential');
        $access_token = $res['access_token'];
        $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
        // if($cid){
        //     $param = json_encode(array("path"=>"pages/zhuli/in/in?uid=".$uid."&cid=".$cid."&type=".$type."&hid=".$hid,"width"=> 50));
        // }else{
        //     $param = json_encode(array("path"=>"pages/mypromote/index/index?uid=".$uid,"width"=> 50));
        // }
        //$param = json_encode(array("path"=>"pages/zhuli/in/in","width"=> 50));
        $result = $this->httpRequest($qcode, $data,"POST");
        $errcode = json_decode($result,true)['errcode'];
        $errmsg = json_decode($result,true)['errmsg'];
        if($errcode) return array('status'=>0,'info'=>$errmsg);
        
        $qr_path = '/rcode/';
        if(!is_dir('.'.$qr_path)){
            mkdir(iconv("GBK","UTF-8",'.'.$qr_path),0777,true);
        }
        $file = '.'.$qr_path.$uid.time().'.png';			
    
        $res = file_put_contents($file,$result);			//	将获取到的二维码图片流保存成图片文件
    
        if($res === false) return array('status'=>0,'info'=>'生成二维码失败'); 
        return $file;
    }

    public function httpRequest($url, $data='', $method='GET'){
        $curl = curl_init();  
        curl_setopt($curl, CURLOPT_URL, $url);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);  
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);  
        if($method=='POST')
        {
            curl_setopt($curl, CURLOPT_POST, 1); 
            if ($data != '')
            {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
            }
        }
    
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);  
        curl_setopt($curl, CURLOPT_HEADER, 0);  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
        $result = curl_exec($curl);  
        curl_close($curl);  
        return $result;
    }

    // 发送access_token
    public function getAccessToken($appid,$secret,$grant_type){
        if (empty($appid)||empty($secret)||empty($grant_type)) {
            return '参数错误';
        }
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type={$grant_type}&appid={$appid}&secret={$secret}";
        $json = $this->https_request($url);
        $data=json_decode($json,true);
        return $data;
    }

    public function https_request($url,$data = null){
        if(function_exists('curl_init')){
          $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
            if (!empty($data)){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }else{
          return false;
        }
    }
}
